package com.softeng.network.chess;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
 
public class ChessPiece {
	
	public String color;
		
	public String pieceString = "";
	
	public String idString;
	
	public boolean checked = false;
	
	public String whichKing;
	
	//-----------------------------------------------------------------------------------------------
	
	ChessPiece(){
	
		//default constructor for a chess piece
		
	}
	
	//-----------------------------------------------------------------------------------------------
	
	public void checkMoves(){
		
		//check for available moves on the board once selected
	
	}
	
	//-----------------------------------------------------------------------------------------------
	
	//Sees if a particular move is a move that a piece is allowed to make
	public boolean validMove(int currentPosition, int position, ChessPiece[] boardArray){ 
		
		//checks if a move is a valid move in relation to the rules of the game of chess
		
		return false;
	
	}
	
	//-----------------------------------------------------------------------------------------------
	
	//Moves the piece to the new specified location
	public void commitMove(int position, JFrame mWindow, int currentPosition, ChessPiece[] boardArray){
		
		boolean valid = false; 
		if(boardArray[currentPosition].color.equals(Chess.getInstance(false).currPlayer)){
			
			//if there's an enemy at the new position, that piece is killed
			if (boardArray[position] != null && !boardArray[position].color.equals(boardArray[currentPosition].color)){
	
				if (boardArray[currentPosition].idString.equals("pawn")){
					System.out.println("first move = " + ((Pawn)boardArray[currentPosition]).firstMove);
					valid = ((Pawn) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("rook")){
					valid = ((Rook) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("bishop")){
					valid = ((Bishop) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("knight")){
					valid = ((Knight) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("king")){
					valid = ((King) boardArray[currentPosition]).validMove(currentPosition, position, boardArray); 
				}
				else if (boardArray[currentPosition].idString.equals("queen")){
					valid = ((Queen) boardArray[currentPosition]).validMove(currentPosition, position, boardArray); 
				}
				else
				{
					valid = false;
				}
				
				if(valid){
					
					//notifies players that the game is over and they've won or lost
					if (boardArray[position].idString.equals("king")){
						
						if (boardArray[position].color.equals(Chess.getInstance(false).oppColor)){
							JOptionPane.showMessageDialog(null, "CHECKMATE!  YOU WIN!!!");
						}
						else{
							JOptionPane.showMessageDialog(null, "CHECKMATE!  YOU LOSE!!!");
						}
						Chess.getInstance(false).mWindow.setVisible(false);
					}
										
					//removes the enemy piece at the new position
					boardArray[currentPosition].removePieceImage(position, mWindow);
										
					//if piece is a pawn that has reached the other side of the board it is promoted-----------------------------------
					if(boardArray[currentPosition].idString.equals("pawn") && (boardArray[currentPosition].color.equals(Chess.getInstance(false).myColor) && getRowFromPos(position) == 8)){
						
						String newPiece;
						boolean finished = false;
						while (!finished){
							newPiece = JOptionPane.showInputDialog("Choose which piece you want to be promoted to: R/K/B/Q");
							//upgrade pawn to the new piece type
							if (newPiece.equalsIgnoreCase("R"))
							{
								Rook r = new Rook(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = r;
								finished = true;
							}
							else if (newPiece.equalsIgnoreCase("K"))
							{
								Knight k = new Knight(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = k;
								finished = true;
							}
							else if (newPiece.equalsIgnoreCase("B"))
							{
								Bishop b = new Bishop(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = b;
								finished = true;
							}
							else if (newPiece.equalsIgnoreCase("Q"))
							{
								Queen q = new Queen(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = q;
								finished = true;
							}	
						}
				        	
					}
					
					//DONT THINK WE NEED THIS SECTION, BUT FOR TESTING PURPOSES, ITS NICE TO HAVE
					//WILL PROBABLY JUST BE ABLE TO DELETE IT LATER
					//SINCE WE CAN'T PICK WHAT THE OPPONENT'S NEW PIECE WILL BE
					//THE BOARD WILL JUST BE UPDATED WITH WHAT THE OPPONENT CHOOSES
					else if(boardArray[currentPosition].idString.equals("pawn") && boardArray[currentPosition].color.equals(Chess.getInstance(false).oppColor) && getRowFromPos(position) == 1){
						
						String newPiece;
						boolean finished = false;
						while (!finished){
							newPiece = JOptionPane.showInputDialog("Choose which piece you want to be promoted to: R/K/B/Q");
							//upgrade pawn to the new piece type
							if (newPiece.equalsIgnoreCase("R"))
							{
								Rook r = new Rook(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = r;
								finished = true;
							}
							else if (newPiece.equalsIgnoreCase("K"))
							{
								Knight k = new Knight(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = k;
								finished = true;
							}
							else if (newPiece.equalsIgnoreCase("B"))
							{
								Bishop b = new Bishop(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = b;
								finished = true;
							}
							else if (newPiece.equalsIgnoreCase("Q"))
							{
								Queen q = new Queen(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
								boardArray[position] = q;
								finished = true;
							}	
						}
						
					}
					//adds your piece to the new position
					else{
						boardArray[currentPosition].addPieceImage(position, mWindow, boardArray[currentPosition].pieceString);
						boardArray[position] = boardArray[currentPosition];
					}
					
					//removes the piece from its original position
					boardArray[currentPosition].removePieceImage(currentPosition, mWindow);
					boardArray[currentPosition] = null;
					
					Chess.getInstance(false).switchPlayer();
					
					if(checkMethod(boardArray)){
						JOptionPane.showMessageDialog(null, "Check!");
					}
					
				}else{
					
					JOptionPane.showMessageDialog(null, "That piece cannot move there.  Please try a different move.");//not this one
				
				}
			
			} 
			//if the new position is empty, the piece is just moved there
			else {
				
				if (boardArray[currentPosition].idString.equals("pawn")){
					System.out.println("first move = " + ((Pawn)boardArray[currentPosition]).firstMove);
					valid = ((Pawn) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("rook")){
					valid = ((Rook) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("bishop")){
					valid = ((Bishop) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("knight")){
					valid = ((Knight) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else if (boardArray[currentPosition].idString.equals("king") && checkMethod(boardArray) == false){
					valid = ((King) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);	
				}
				else if (boardArray[currentPosition].idString.equals("queen")){
					valid = ((Queen) boardArray[currentPosition]).validMove(currentPosition,position,boardArray);
				}
				else{
					valid = false;
				}
				
				//if piece is a pawn that has reached the other side of the board it is promoted-----------------------------------
				if(boardArray[currentPosition].idString.equals("pawn") && (boardArray[currentPosition].color.equals(Chess.getInstance(false).myColor) && getRowFromPos(position) == 8)){
					
					String newPiece;
					boolean finished = false;
					while (!finished){
						newPiece = JOptionPane.showInputDialog("Choose which piece you want to be promoted to: R/K/B/Q");
						//upgrade pawn to the new piece type
						if (newPiece.equalsIgnoreCase("R"))
						{
							Rook r = new Rook(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = r;
							finished = true;
						}
						else if (newPiece.equalsIgnoreCase("K"))
						{
							Knight k = new Knight(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = k;
							finished = true;
						}
						else if (newPiece.equalsIgnoreCase("B"))
						{
							Bishop b = new Bishop(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = b;
							finished = true;
						}
						else if (newPiece.equalsIgnoreCase("Q"))
						{
							Queen q = new Queen(Chess.getInstance(false).myColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = q;
							finished = true;
						}	
					}
					
					//removes the piece from its original position
					boardArray[currentPosition].removePieceImage(currentPosition, mWindow);	
					boardArray[currentPosition] = null;
					Chess.getInstance(false).switchPlayer();
					if(checkMethod(boardArray)){
						JOptionPane.showMessageDialog(null, "Check!");
					}				
				}
				//DONT THINK WE NEED THIS SECTION, BUT FOR TESTING PURPOSES, ITS NICE TO HAVE
				//WILL PROBABLY JUST BE ABLE TO DELETE IT LATER
				//SINCE WE CAN'T PICK WHAT THE OPPONENT'S NEW PIECE WILL BE
				//THE BOARD WILL JUST BE UPDATED WITH WHAT THE OPPONENT CHOOSES
				else if(boardArray[currentPosition].idString.equals("pawn") && boardArray[currentPosition].color.equals(Chess.getInstance(false).oppColor) && getRowFromPos(position) == 1){
					
					String newPiece;
					boolean finished = false;
					while (!finished){
						newPiece = JOptionPane.showInputDialog("Choose which piece you want to be promoted to: R/K/B/Q");
						//upgrade pawn to the new piece type
						if (newPiece.equalsIgnoreCase("R"))
						{
							Rook r = new Rook(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = r;
							finished = true;
						}
						else if (newPiece.equalsIgnoreCase("K"))
						{
							Knight k = new Knight(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = k;
							finished = true;
						}
						else if (newPiece.equalsIgnoreCase("B"))
						{
							Bishop b = new Bishop(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = b;
							finished = true;
						}
						else if (newPiece.equalsIgnoreCase("Q"))
						{
							Queen q = new Queen(Chess.getInstance(false).oppColor,position,Chess.getInstance(false).mWindow);
							boardArray[position] = q;
							finished = true;
						}	
					}
					
					//removes the piece from its original position
					boardArray[currentPosition].removePieceImage(currentPosition, mWindow);
					boardArray[currentPosition] = null;
					Chess.getInstance(false).switchPlayer();
					if(checkMethod(boardArray)){
						JOptionPane.showMessageDialog(null, "Check!");
					}
					
				}
		       
				if(valid){
					
					//en passant movement up the board (my en passant)
					if (boardArray[currentPosition].idString.equals("pawn") && getRowFromPos(position) == (getRowFromPos(currentPosition) + 1) && ((getColFromPos(position) == (getColFromPos(currentPosition) + 1) || getColFromPos(position) == (getColFromPos(currentPosition) - 1))) && boardArray[position] == null){
						
						if (boardArray[currentPosition + 1] != null && boardArray[currentPosition + 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition + 1]).twoSquaresOnFirst == true){
							//removes the opponent pawn that made the move 2 down on its first move
							boardArray[currentPosition].removePieceImage((position+8), mWindow);
							boardArray[position+8] = null;
						}
						else if (boardArray[currentPosition - 1] != null && boardArray[currentPosition - 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition - 1]).twoSquaresOnFirst == true){
							//removes the opponent pawn that made the move 2 down on its first move
							boardArray[currentPosition].removePieceImage((position+8), mWindow);
							boardArray[position+8] = null;
						}
						
					}
					//en passant movement down the board (opponent en passant)
					else if (boardArray[currentPosition].idString.equals("pawn") && getRowFromPos(position) == (getRowFromPos(currentPosition) - 1) && ((getColFromPos(position) == (getColFromPos(currentPosition) + 1) || getColFromPos(position) == (getColFromPos(currentPosition) - 1))) && boardArray[position] == null){
						
						if (boardArray[currentPosition + 1] != null && boardArray[currentPosition + 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition + 1]).twoSquaresOnFirst == true){
							//removes my pawn that made the move 2 up on its first move
							boardArray[currentPosition].removePieceImage((position-8), mWindow);
							boardArray[position-8] = null;
						}
						else if (boardArray[currentPosition - 1] != null && boardArray[currentPosition - 1].idString.equals("pawn") && ((Pawn)boardArray[currentPosition - 1]).twoSquaresOnFirst == true){
							//removes my pawn that made the move 2 up on its first move
							boardArray[currentPosition].removePieceImage((position-8), mWindow);
							boardArray[position-8] = null;
						}
						
					}
					
					//adds the piece clicked to the new empty position
					boardArray[currentPosition].addPieceImage(position, mWindow, boardArray[currentPosition].pieceString);
										
					if (Chess.getInstance(false).myColor.equals("white")){
					
						//Castling to the right
						if (boardArray[position] == null && boardArray[currentPosition].idString.equals("king") && ((King)boardArray[currentPosition]).firstMove == true && currentPosition == (position - 2) && boardArray[currentPosition+3].idString.equals("rook") && ((Rook)boardArray[currentPosition+3]).firstMove == true && !((King)boardArray[currentPosition]).piecesBetweenKingRook(currentPosition,currentPosition+3,boardArray)){
							//adds the Rook to the new position
							boardArray[currentPosition+3].addPieceImage(currentPosition+1, mWindow, boardArray[currentPosition+3].pieceString);
							boardArray[currentPosition+1] = boardArray[currentPosition+3];
							//removes Rook from its original position
							boardArray[currentPosition+3].removePieceImage(currentPosition+3, mWindow);
							boardArray[currentPosition+3] = null;
						}
						//Castling to the left
						else if (boardArray[position] == null && boardArray[currentPosition].idString.equals("king") && ((King)boardArray[currentPosition]).firstMove == true && currentPosition == (position + 2) && boardArray[currentPosition-4].idString.equals("rook") && ((Rook)boardArray[currentPosition-4]).firstMove == true && !((King)boardArray[currentPosition]).piecesBetweenKingRook(currentPosition,currentPosition-4,boardArray)){
							//adds the Rook to the new position
							boardArray[currentPosition-4].addPieceImage(currentPosition-1, mWindow, boardArray[currentPosition-4].pieceString);
							boardArray[currentPosition-1] = boardArray[currentPosition-4];
							//removes Rook from its original position
							boardArray[currentPosition-4].removePieceImage(currentPosition-4, mWindow);
							boardArray[currentPosition-4] = null;
						}
						
					}
					else if (Chess.getInstance(false).myColor.equals("black")){
						
						//Castling to the right
						if (boardArray[position] == null && boardArray[currentPosition].idString.equals("king") && ((King)boardArray[currentPosition]).firstMove == true && currentPosition == (position - 2) && boardArray[currentPosition+4].idString.equals("rook") && ((Rook)boardArray[currentPosition+4]).firstMove == true && !((King)boardArray[currentPosition]).piecesBetweenKingRook(currentPosition,currentPosition+4,boardArray)){
							//adds the Rook to the new position
							boardArray[currentPosition+4].addPieceImage(currentPosition+1, mWindow, boardArray[currentPosition+4].pieceString);
							boardArray[currentPosition+1] = boardArray[currentPosition+4];
							//removes Rook from its original position
							boardArray[currentPosition+4].removePieceImage(currentPosition+4, mWindow);
							boardArray[currentPosition+4] = null;
						}
						//Castling to the left
						else if (boardArray[position] == null && boardArray[currentPosition].idString.equals("king") && ((King)boardArray[currentPosition]).firstMove == true && currentPosition == (position + 2) && boardArray[currentPosition-3].idString.equals("rook") && ((Rook)boardArray[currentPosition-3]).firstMove == true && !((King)boardArray[currentPosition]).piecesBetweenKingRook(currentPosition,currentPosition-3,boardArray)){
							//adds the Rook to the new position
							boardArray[currentPosition-3].addPieceImage(currentPosition-1, mWindow, boardArray[currentPosition-3].pieceString);
							boardArray[currentPosition-1] = boardArray[currentPosition-3];
							//removes Rook from its original position
							boardArray[currentPosition-3].removePieceImage(currentPosition-3, mWindow);
							boardArray[currentPosition-3] = null;
						}
						
					}
					
					boardArray[position] = boardArray[currentPosition];	
					//removes the piece from its original position
					boardArray[currentPosition].removePieceImage(currentPosition, mWindow);
					boardArray[currentPosition] = null;

					Chess.getInstance(false).switchPlayer();
					
					if(checkMethod(boardArray)){
						JOptionPane.showMessageDialog(null, "Check!");
					}
					
				}
				else{
					
					JOptionPane.showMessageDialog(null, "Selected piece cannot move there.  Please try a different move.");
				
				}
			}
		}
					
	}
	
	//-------------------------------------------------------------------------------------------------------------------
	
	/*
	public boolean moveIntoCheck(int currentPosition, int position, ChessPiece[] boardArray){
		
		boolean intoCheck = false;
		
		if(boardArray[currentPosition].idString.equals("king")){ 
				
			boardArray[currentPosition].commitMove(position, Chess.getInstance(false).mWindow, currentPosition, boardArray);	
			if(checkMethod(boardArray)){
				intoCheck = true;
			}
		}
			
		return intoCheck;
		
	}
	*/
	
	//Sees if the opposing king is now in check
	public boolean checkMethod(ChessPiece[] boardArray){
		
		checked = false;
		// Checks each piece to see if it's putting the king in check
		for (int i = 0; i < boardArray.length; i++){
			if(boardArray[i] != null){
				handlePieceType(boardArray[i].color, boardArray[i].idString, i, boardArray);
				if(checked){
					break;
				}
			}else{
				System.out.println("nothing at position " + i);
			}
		}
		
		return checked;
	
	}
	
	// Describes how a king would be "checked" by each piece type
	public void handlePieceType(String color, String pieceType, int index, ChessPiece[] boardArray){
		
		int positionMyK = findKing(Chess.getInstance(false).myColor, boardArray);
		int positionOppK = findKing(Chess.getInstance(false).oppColor, boardArray);

		int myKingRow = getRowFromPos(positionMyK);
		int oppKingRow = getRowFromPos(positionOppK);
		System.out.println(oppKingRow);
		int myKingCol = getColFromPos(positionMyK);
		int oppKingCol = getColFromPos(positionOppK);
		System.out.println(oppKingCol);
		
		System.out.println(getRowFromPos(index) + " " + getColFromPos(index));
		
		//Sees if your pawn is checking the opponent king
		if(color.equals(Chess.getInstance(false).myColor) && pieceType.equals("pawn")){

			if((oppKingRow == getRowFromPos(index) + 1) && ((oppKingCol == getColFromPos(index) + 1) || (oppKingCol == getColFromPos(index) - 1))){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
		
		}
		//Sees if an opponent pawn is checking your king
		if(color.equals(Chess.getInstance(false).oppColor) && pieceType.equals("pawn")){
			
			if((myKingRow == getRowFromPos(index) - 1) && (myKingCol == getColFromPos(index) + 1 || oppKingCol == getColFromPos(index) - 1)){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
		
		}
		//Sees if your knight is checking the opponent king
		if(color.equals(Chess.getInstance(false).myColor) && pieceType.equals("knight")){
			
			if(((oppKingRow == getRowFromPos(index) - 1) || (oppKingRow == getRowFromPos(index) + 1)) && ((oppKingCol == getColFromPos(index) + 2) || (oppKingCol == getColFromPos(index) - 2))){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			if(((oppKingRow == getRowFromPos(index) - 2) || (oppKingRow == getRowFromPos(index) + 2)) && ((oppKingCol == getColFromPos(index) + 1) || (oppKingCol == getColFromPos(index) - 1))){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
		
		}
		//Sees if an opponent knight is checking your king
		if(color.equals(Chess.getInstance(false).oppColor) && pieceType.equals("knight")){
			
			if(((myKingRow == getRowFromPos(index) - 1) || (myKingRow == getRowFromPos(index) + 1)) && ((myKingCol == getColFromPos(index) + 2) || (myKingCol == getColFromPos(index) - 2))){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			if(((myKingRow == getRowFromPos(index) - 2) || (myKingRow == getRowFromPos(index) + 2)) && ((myKingCol == getColFromPos(index) + 1) || (myKingCol == getColFromPos(index) - 1))){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			
		}
		//Sees if your rook is checking the opponent king
		if(color.equals(Chess.getInstance(false).myColor) && pieceType.equals("rook")){
			
			if((getColFromPos(index) == oppKingCol || getRowFromPos(index) == oppKingRow) && ((Rook)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			
		}
		//Sees if an opponent rook is checking your king
		if(color.equals(Chess.getInstance(false).oppColor) && pieceType.equals("rook")){
			
			if((getColFromPos(index) == myKingCol || getRowFromPos(index) == myKingRow) && ((Rook)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			
		}
		//Sees if your bishop is checking the opponent king
		if(color.equals(Chess.getInstance(false).myColor) && pieceType.equals("bishop")){
			
			if((oppKingRow - getRowFromPos(index)) == (oppKingCol - getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			else if((oppKingRow + getRowFromPos(index)) == (oppKingCol + getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			else if((oppKingRow - getRowFromPos(index)) == -(oppKingCol - getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			else if((oppKingRow + getRowFromPos(index)) == -(oppKingCol + getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			
		}
		//Sees if an opponent bishop is checking your king
		if(color.equals(Chess.getInstance(false).oppColor) && pieceType.equals("bishop")){
			
			if((myKingRow - getRowFromPos(index)) == (myKingCol - getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			else if((myKingRow + getRowFromPos(index)) == (myKingCol + getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			else if((myKingRow - getRowFromPos(index)) == -(myKingCol - getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			else if((myKingRow + getRowFromPos(index)) == -(myKingCol + getColFromPos(index)) && ((Bishop)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			
		}
		//Sees if your queen is checking the opponent king
		if(color.equals(Chess.getInstance(false).myColor) && pieceType.equals("queen")){
			
			//checking for vertical or horizontal "checks"
			if((getRowFromPos(index) == oppKingRow || getColFromPos(index) == oppKingCol) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			//checking for diagonal "checks"
			if((oppKingRow - getRowFromPos(index)) == (oppKingCol - getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			else if((oppKingRow - getRowFromPos(index)) == -(oppKingCol - getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			/*
			//Don't think we need these
			if((oppKingRow + getRowFromPos(index)) == (oppKingCol + getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}
			if((oppKingRow + getRowFromPos(index)) == -(oppKingCol + getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionOppK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).oppColor;
			}*/
			
		}
		
		//Sees if the opponent queen is checking your king
		if(color.equals(Chess.getInstance(false).oppColor) && pieceType.equals("queen")){
			
			//checking for vertical or horizontal "checks"
			if((getRowFromPos(index) == myKingRow || getColFromPos(index) == myKingCol) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			//checking for diagonal "checks"
			if((myKingRow - getRowFromPos(index)) == (myKingCol - getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			else if((myKingRow - getRowFromPos(index)) == -(myKingCol - getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			/*
			//Don't think we need these
			if((myKingRow + getRowFromPos(index)) == (myKingCol + getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}
			if((myKingRow + getRowFromPos(index)) == -(myKingCol + getColFromPos(index)) && ((Queen)boardArray[index]).checkPiecesBetween(index, positionMyK, boardArray) == false){
				checked = true;
				whichKing = Chess.getInstance(false).myColor;
			}*/
			
		}
		
		System.out.println(checked);
		
	}
	
	// Returns the position of the piece
	public int findKing(String clr, ChessPiece[] boardArray){
		
		int piecePosition = 0;
		
		for(int i = 0; i < boardArray.length; i++){
			
			if(boardArray[i] != null && boardArray[i].color.equals(clr) && boardArray[i].idString.equals("king")){
				piecePosition = i;
				break;
			}
	
		}
		
		return piecePosition;
	}
	
	//-------------------------------------------------------------------------------------------------------------------
	
	//removes the piece image at the position
	public void removePieceImage(int position, JFrame window){
		
		JPanel panel1 = (JPanel) window.getContentPane().getComponent(position);
		panel1.remove(panel1.getComponent(0)); 
		window.validate();
    	window.repaint();
    	
	}
	
	//-----------------------------------------------------------------------------------------------
	
	//adds the image of the piece to the position
	public void addPieceImage(int position, JFrame window, String imageID){
		
		JLabel piece1 = new JLabel(new ImageIcon(imageID));
    	JPanel panel1 = (JPanel) window.getContentPane().getComponent(position);
    	panel1.setBorder(new EmptyBorder(5, 5, 5, 5));
    	panel1.add(piece1);
    	
    	window.validate();
    	window.repaint();
    	
	}
	
	//-----------------------------------------------------------------------------------------------
	
	public int getRowFromPos(int position){
		
		int row = 0; 
		
		if(position < 8){
			row = 8;
		}
		else if(position > 7 && position < 16){
			row = 7;
		}
		else if(position > 15 && position < 24){
			row = 6; 
		}
		else if(position > 23 && position < 32){
			row = 5;
		}
		else if(position > 31 && position < 40){
			row = 4;
		}
		else if(position >39 && position < 48){
			row = 3; 
		}
		else if(position > 47 && position < 56){
			row = 2; 
		}
		else{
			row = 1; 
		}
			
		
		return row; 
	}
	
	//-----------------------------------------------------------------------------------------------
	
	public int getColFromPos(int position){
		
		int col = 0;
		
		if(position==0||position ==8||position ==16||position ==24||position ==32||position ==40||position ==48||position ==56){
			col = 1;
		}
		else if(position==1||position ==9||position ==17||position ==25||position ==33||position ==41||position ==49||position ==57){
			col = 2;
		}
		else if(position==2||position ==10||position ==18||position ==26||position ==34||position ==42||position ==50||position ==58){
			col = 3; 
		}
		else if(position==3||position ==11||position ==19||position ==27||position ==35||position ==43||position ==51||position ==59){
			col = 4;
		}
		else if(position==4||position ==12||position ==20||position ==28||position ==36||position ==44||position ==52||position ==60){
			col = 5;
		}
		else if(position==5||position ==13||position ==21||position ==29||position ==37||position ==45||position ==53||position ==61){
			col = 6;
		}
		else if(position==6||position ==14||position ==22||position ==30||position ==38||position ==46||position ==54||position ==62){
			col = 7;
		}
		else if(position==7||position ==15||position ==23||position ==31||position ==39||position ==47||position ==55||position ==63){
			col = 8; 
		}
		
		return col; 
		
	}
	
	//-----------------------------------------------------------------------------------------------
	public int rowCol2Position(int Row, int Col){
		
		int position = -1;
		
		if(Row == 8){
			position = (Col - 1); 
		}
		else if(Row == 7){
			position = (Col - 1 + 8); 
		}
		else if(Row == 6){
			position = (Col - 1 + 16); 
		}
		else if(Row == 5){
			position = (Col - 1 + 24); 
		}
		else if(Row == 4){
			position = (Col - 1 + 32); 
		}
		else if(Row == 3){
			position = (Col - 1 + 40); 
		}
		else if(Row == 2){
			position = (Col - 1 + 48); 
		}
		else if(Row == 1){
			position = (Col - 1 + 56); 
		}		
		
		return position; 
	}
	
	//-----------------------------------------------------------------------------------------------------------------------------
	
}
